﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Xml;

namespace GraphTools
{
    public static class GPXHandler
    {
        public static List<Node> ReadNodesFromGPX(string file)
        {
            XDocument doc = XDocument.Load(file);
            List<Node> result = new List<Node>();

            foreach (XElement child in doc.Root.Elements("wpt"))
            {
                //double lon = Decimal.Parse(child.Attribute("lon").Value.Replace('.', ','));
                Node tmp = new Node((float)XmlConvert.ToDouble(child.Attribute("lat").Value),
                                    (float)XmlConvert.ToDouble(child.Attribute("lon").Value));
                result.Add(tmp);
            }
            return result;
        }

        public static void WriteNodesToGPX(string file, List<Node> points)
        {
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            //settings.NewLineOnAttributes = true;
            using (XmlWriter writer = XmlWriter.Create(file, settings))
            {
                writer.WriteStartDocument();
                writer.WriteStartElement("gpx");
                foreach (Node n in points)
                {
                    writer.WriteStartElement("wpt");
                    writer.WriteAttributeString("lat", XmlConvert.ToString(n.Latitude));
                    writer.WriteAttributeString("lon", XmlConvert.ToString(n.Longitude));
                    writer.WriteEndElement();
                }
                writer.WriteEndElement();
                writer.WriteEndDocument();
            }
        }

        public static void WriteEdgesToGPX(string file, List<Edge> edges)
        {
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            //settings.NewLineOnAttributes = true;
            using (XmlWriter writer = XmlWriter.Create(file, settings))
            {
                writer.WriteStartDocument();
                writer.WriteStartElement("gpx");
                    writer.WriteStartElement("trk");
                    foreach (Edge e in edges)
                    {
                        writer.WriteStartElement("trkseg");
                            //Node1
                        writer.WriteStartElement("trkpt");
                            writer.WriteAttributeString("lat", XmlConvert.ToString(e.Node1.Latitude));
                            writer.WriteAttributeString("lon", XmlConvert.ToString(e.Node1.Longitude));
                            writer.WriteEndElement();
                            
                            //Node2
                            writer.WriteStartElement("trkpt");
                            writer.WriteAttributeString("lat", XmlConvert.ToString(e.Node2.Latitude));
                            writer.WriteAttributeString("lon", XmlConvert.ToString(e.Node2.Longitude));
                            writer.WriteEndElement();
                        writer.WriteEndElement();
                    }
                    writer.WriteEndElement();
                writer.WriteEndElement();
                writer.WriteEndDocument();
            }
        }
    }
}
